import 'package:barcode_scan/barcode_scan.dart';
import 'package:flutter/material.dart';
import 'package:fyi/dao/bind_dao.dart';
import 'package:fyi/event/event.dart';
import 'package:fyi/utils/bus_util.dart';
import 'package:fyi/utils/common_util.dart';
import 'package:fyi/utils/navigator_util.dart';

class BindPage extends StatefulWidget {
  @override
  _BindPageState createState() => _BindPageState();
}

class _BindPageState extends State<BindPage> {

  final TextEditingController _controller = TextEditingController();

  String _scanId = null;

  @override
  void initState() {
    super.initState();
    _controller.text = "苏MM7777";
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
            color: Color.fromARGB(255, 62, 146, 203)
        ),
        child: ListView(
          children: <Widget>[
            _logoView(),
            _content()
          ],
        ),
      ),
    );
  }

  Widget _logoView() {
    return Container(
      alignment: Alignment.center,
      margin: EdgeInsets.only(top: 42,bottom: 28),
      decoration: BoxDecoration(
        color: Colors.transparent
      ),
      child: Image.asset("assets/images/ic_tag.png",width:180,height: 68),
    );
  }

  Widget _content() {
    return Container(
      margin: EdgeInsets.only(left: 16,right: 16,bottom: 42),
      child: PhysicalModel(
        color: Color.fromARGB(255, 246, 246, 246),
        borderRadius: BorderRadius.circular(20),
        child: Column(
          children: <Widget>[
            Image.asset("assets/images/bind_bg.png"),
            Container(
              margin: EdgeInsets.only(left: 16,right: 16,top: 22,bottom: 8),
              color: Colors.white,
              child: new TextField(
                controller: _controller,
                decoration: new InputDecoration(
                    border: new OutlineInputBorder(  //添加边框
                        gapPadding: 10.0,
                        borderRadius: BorderRadius.circular(0.0),
                        borderSide: BorderSide(width: 0,color: Colors.white,style: BorderStyle.none)
                    ),
                    hintText: CommonUtil.getS(context).enter_license_plate,
                    contentPadding: const EdgeInsets.all(15.0)),
                keyboardType: TextInputType.text,
              ),
            ),
            _wrapTap(Container(
              margin: EdgeInsets.fromLTRB(16, 8, 16, 8),
              padding: EdgeInsets.fromLTRB(16, 12, 16, 12),
              color: Colors.white,
              child: Row(
                children: <Widget>[
                  Expanded(child: Text(CommonUtil.getS(context).scan_headstock_qr,style: TextStyle(color: Colors.black,fontSize: 14,decoration: TextDecoration.none),)),
                  Image.asset("assets/images/ic_qr.png",height: 40,width: 40,)
                ],
              ),
            ), (){
              BarcodeScanner.scan().then((value){
                print('scan value : ${value}');
                _scanId = value;
              }).catchError((err){
                print('scan err : ${err}');
              });
            }),
            _wrapTap(Container(
              margin: EdgeInsets.fromLTRB(16, 8, 16, 8),
              padding: EdgeInsets.fromLTRB(16, 12, 16, 12),
              color: Colors.white,
              child: Row(
                children: <Widget>[
                  Expanded(child: Text(CommonUtil.getS(context).scan_carriage_qr,style: TextStyle(color: Colors.black,fontSize: 14,decoration: TextDecoration.none),)),
                  Image.asset("assets/images/ic_qr.png",height: 40,width: 40,)
                ],
              ),
            ), (){
              BarcodeScanner.scan().then((value){
                print('scan value : ${value}');
                _scanId = value;
              }).catchError((err){
                print('scan err : ${err}');
              });
            }),
            _wrapTap(Container(
              width: 120,
              height: 48,
              margin: EdgeInsets.only(top: 28,bottom: 16),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(24),
                  color:Colors.blue
              ),
              child: Center(
                child: Text(CommonUtil.getS(context).bind,style: TextStyle(color: Colors.white,fontSize: 16,decoration: TextDecoration.none),),
              ),
            ), (){
              _bindAction(context);
            }),
          ],
        ),
      ),
    );
  }

  _wrapTap(Container container, void Function() click) {
    return GestureDetector(
      child: container,
      onTap: (){
        if(click != null){
          click();
        }
      },
    );
  }

  void _bindAction(BuildContext context) {
    String carNo = _controller.text;
    if(carNo == ""){
      CommonUtil.toast(CommonUtil.getS(context).enter_license_plate);
      return;
    }
    if(_scanId == null || _scanId == ""){
      CommonUtil.toast(CommonUtil.getS(context).scan_qr);
      return;
    }
    _bind(carNo, "", "");
  }

  _bind(String licensePlate,String headerNo,String carriageNo){
    BindDao.bind(licensePlate, headerNo, carriageNo).then((value){
      print("value 2 : ${value}");
      if(value){
        NavigatorUtil.pop(context);
        BusUtil.eventBus.fire(RefreshHistoryEvent());
      }
    }).catchError((err){
      print("err 2 : ${err}");
    });
  }
}
